package rcs34.android.Namedays.Widget;

import java.util.GregorianCalendar;

import rcs34.android.Namedays.GApp;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class WidgetProvider extends AppWidgetProvider {
  public static final String ALARM_ACTION = "rcs34.android.Namedays.ALARM_ACTION";
  public static final String PREF_ACTION = "rcs34.android.Namedays.PREF_ACTION";
  protected final String LOGTAG = getClass().getName();
  
  @SuppressLint("NewApi")
  @Override
  public void onEnabled(Context _context) {
    /*		if (android.os.Build.VERSION.SDK_INT >= 9) {
    			Log.w(LOGTAG, "Strict mode will be set");
    			android.os.StrictMode
    					.setThreadPolicy(new android.os.StrictMode.ThreadPolicy.Builder()
    							.detectAll().penaltyDeath().build());
    			android.os.StrictMode
    					.setVmPolicy(new android.os.StrictMode.VmPolicy.Builder()
    							.detectAll().penaltyDeath().build());
    		}
    		*/
    AlarmManager am = (AlarmManager) _context
        .getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(ALARM_ACTION);
    PendingIntent pi = PendingIntent.getBroadcast(_context, 0, i, 0);
    GregorianCalendar now = new GregorianCalendar();
    am.set(AlarmManager.RTC, now.getTimeInMillis(), pi);
  }
  
  @Override
  public void onDeleted(Context _context, int[] _ids) {
    for (int i = 0; i < _ids.length; i++) {
      SharedPreferences  prefs = GApp.getWidgetPreferences(_ids[i]);
      Editor editor = prefs.edit();
      editor.clear();
      editor.commit();
    }
  }
  
  @Override
  public void onDisabled(Context _context) {
    Intent i = new Intent(ALARM_ACTION);
    PendingIntent pi = PendingIntent.getBroadcast(_context, 0, i, 0);
    AlarmManager am = (AlarmManager) _context
        .getSystemService(Context.ALARM_SERVICE);
    am.cancel(pi);
  }
  
  @Override
  public void onReceive(Context _context, Intent intent) {
    if (intent.getAction().equals(ALARM_ACTION)
        || intent.getAction().equals(Intent.ACTION_TIME_CHANGED)
        || intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
        || intent.getAction().equals(Intent.ACTION_USER_PRESENT)
        || intent.getAction().equals(PREF_ACTION)) {
      AppWidgetManager manager = AppWidgetManager.getInstance(_context);
      ComponentName provider = new ComponentName(_context.getPackageName(),
          getClass().getName());
      int[] ids = manager.getAppWidgetIds(provider);
      if (ids != null && ids.length > 0) {
        // manager.notifyAppWidgetViewDataChanged(ids, R.id.daysView);
        GregorianCalendar now = new GregorianCalendar();
        GApp.isALeepYear=now.isLeapYear(now.get(GregorianCalendar.YEAR));
        // now.add(GregorianCalendar.SECOND, 20);
        now.add(GregorianCalendar.DAY_OF_YEAR, 1);
        now.set(GregorianCalendar.HOUR_OF_DAY, 0);
        now.set(GregorianCalendar.MINUTE, 0);
        now.set(GregorianCalendar.SECOND, 1);
        now.set(GregorianCalendar.MILLISECOND, 0);
        Intent i = new Intent(ALARM_ACTION);
        PendingIntent pi = PendingIntent.getBroadcast(_context, 0, i, 0);
        AlarmManager am = (AlarmManager) _context
            .getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC, now.getTimeInMillis(), pi);
        onUpdate(_context, manager, ids);
      }
    }
  }
  
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {
    if (appWidgetIds != null && appWidgetIds.length > 0) {
      // Build the intent to call the service
      Intent intent = new Intent(context.getApplicationContext(),
          WidgetService.class);
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
      // Update the widgets via the service
      context.startService(intent);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
  }

}
